<template>
	<view >
		<u-popup v-model="loginPopup" mode="center" border-radius="30">
			<view class="uni-tip">
				<view class="uni-tip-title">您还未登录</view>
				<view class="uni-tip-content">请先登录之后再进行操作</view>
				<view class="uni-tip-icon"><image class="login-img" src="https://file.diyanweilai.com/icon%2Fnot_logo.png" mode="widthFix"></image></view>
				<view class="uni-tip-group-button">
					<button type="default" class="uni-tip-button ns-text-color-gray" @click="noLogin">暂不登录</button>
					<!-- #ifdef MP-WEIXIN  -->						
					<button v-if="canIUseGetUserProfile" type="primary" open-type="getUserInfo" @click="bindgetuserinfo" class="uni-tip-button ns-bg-color">立即登录</button>
					<button v-else type="primary" open-type="getUserInfo" @getuserinfo="bindgetuserinfo" class="uni-tip-button ns-bg-color">立即登录</button>
					<!-- #endif  -->
					<!-- #ifdef H5 -->
					<button type="primary" @click="tologin" class="uni-tip-button ns-bg-color">立即登录</button>
					<!-- #endif  -->
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		name:"ns-login",
		components: { },
		props: {
			href: ''
		},
		data() {
			return {
				canIUseGetUserProfile: false,
				avatar : "",
				nickname : "",
				loginPopup:false,
			};
		},
		created() {
			// #ifdef MP-WEIXIN
			if (wx.getUserProfile) this.canIUseGetUserProfile = true;
			// #endif
		},
		methods: {
			noLogin() {
				this.loginPopup = false;
			},
			clickLogin() {
				// #ifdef H5 
					this.$util.redirectTo('/pages/login/login', { goback: this.href });
				// #endif
				// #ifdef MP-WEIXIN 
				this.loginPopup = true;
				// #endif
			},
			// 小程序获取授权登录
			bindgetuserinfo(e) {
				console.log(e);
				this.loginPopup = false;
				this.getWxUser(e);
			},
			tologin(){
				this.loginPopup = false;
				this.$util.redirectTo('/pages/login/login', { goback: this.href });
			},
			// 获取个人信息
			getWxUser(e) {
				// 获取服务商
				var provider = [];
				uni.getProvider({
					service: 'oauth',
					success: res => {
						provider = res.provider;
					}
				});
				// #ifdef MP-WEIXIN
				if(this.canIUseGetUserProfile){
					uni.getUserProfile({
						desc: '获取用户基本资料',
					    lang: 'zh_CN',
						success: (res) => {
							if (res.errMsg == 'getUserProfile:ok') {					
								this.avatar = res.userInfo.avatarUrl;
								this.nickname = res.userInfo.nickName;
								this.wxLogin(provider);
							}else {
								this.$util.showToast({ title: res.errMsg });
								setTimeout(() => {
									this.$util.reLaunch('/pages/member/member', {}, 'tabbar');
								}, 1500);
							}
						}
					})	
				}else{
					if(e.detail.errMsg='getUserInfo:ok'){
						this.avatar = e.detail.userInfo.avatarUrl;
						this.nickname = e.detail.userInfo.nickName;
						this.wxLogin(provider);
					}else {
						this.$util.showToast({ title:e.detail.errMsg });
						setTimeout(() => {
							this.$util.reLaunch('/pages/member/member', {}, 'tabbar');
						}, 1500);
					}
				}	
				// #endif
			},
			// 微信登录
			wxLogin(provider){
				if (provider[0] != undefined) {
					uni.login({
						provider: provider[0],
						timeout: 3000,
						scopes: 'auth_user',
						success: res => {
							if (res.errMsg == 'login:ok') {
								let data={
									userLogo: this.avatar,
									nickName: this.nickname,
									weCode: res.code,
								}
								switch (provider[0]) {
									case 'weixin':
									this.$post({
										url: 'wechat/wechatLogin',
										data: data,
										header:{
											'content-type': 'application/json'
										},
									}).then(res => {
										console.log(res);
										if(res.data&&res.data.token){
											uni.setStorageSync('token',res.data.token);
											uni.setStorageSync('userInfo',{userLogo: this.avatar,nickName: this.nickname});
											// this.$util.showToast({
											// 	title: res.msg
											// });
											// setTimeout(()=>{
												this.$reloadPage();
											// },200)
										}
									},(err)=>{
										this.$util.showToast({
											title: err.msg
										});
									});
									break;
								}
							} else {
								this.$util.showToast({
									title: res.errMsg
								});
							}
						},
						fail: res => {
							console.log(res);
						}
					});
				}
			},
		}
	}
</script>

<style scoped lang="scss">
.uni-tip {
	width: 600rpx;
	background: #fff;
	box-sizing: border-box;
	border-radius: 20rpx;
	overflow: hidden;
	height: initial;
}

.uni-tip-title {
	text-align: center;
	font-weight: bold;
	font-size: $ns-font-size-lg;
	padding: $ns-padding;
}

.uni-tip-content {
	text-align: center;
	padding: 10rpx 0;
	color: #666;
}

.uni-tip-icon {
	width: 100%;
	text-align: center;
	.login-img {
		width: 300rpx;
	}
}
.uni-tip-group-button {
	margin-top: 20rpx;
	line-height: 120rpx;
	display: flex;
	border-top: 2rpx solid $ns-border-color-gray;
}

.uni-tip-button {
	width: 100%;
	text-align: center;
	border: none;
	border-radius: 0;
	padding: 0;
	margin: 0;
	background: #fff;
}

.uni-tip-button:after {
	border: none;
}
</style>
